//
//  PayViewController.m
//  何五路
//
//  Created by _ADY on 15/7/17.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "PayViewController.h"

@interface PayViewController ()

@end
#define payHight  60
@implementation PayViewController
@synthesize mTableView,allString,orderId,urlStr;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    mTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width, screenMySize.size.height) style:UITableViewStyleGrouped];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.sectionHeaderHeight = 0.0;
    mTableView.sectionFooterHeight = 0.0;
    mTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mTableView];
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
    [mTableView setTableFooterView:v];
    
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.view.backgroundColor = bgColor;
    self.title = @"何五路收银台";
    BOOL isPay = [[NSUserDefaults standardUserDefaults] boolForKey:@"isPayView"];
    if (isPay)
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isPayView"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"是否确定完成支付？"  delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是",nil];
        [alert show];
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [self.navigationController popViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:kTLement object:self];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    BOOL isPay = [[NSUserDefaults standardUserDefaults] boolForKey:@"isPayView"];
    if (!isPay)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:kTLement object:self];
    }
}

#pragma mark -
#pragma mark Table View DataSource

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    //    if (section == 0)
    //        return 0.001;
    return 10;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return payHight/2;
    }
    if (indexPath.row == 1 || indexPath.row == 3) {
        return 0;
    }
    return payHight;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"AllorderCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        aTableView.showsVerticalScrollIndicator = NO;
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    cell.backgroundColor = [UIColor whiteColor];
    
    if (indexPath.row != 0)
    {
        if (indexPath.row == 1 || indexPath.row == 3) {
            return cell;
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        NSArray *tArray = [NSArray arrayWithObjects:@"何五路支付",@"快捷支付",@"微信支付", nil];
        NSArray *aArray = [NSArray arrayWithObjects:@"首页使用，满19元立减10块",@"快捷支付服务",@"微信安全支付", nil];
        
        UIImageView *bg1ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,(payHight-34)/2, 34,34)];
        [bg1ImageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Pay%d",indexPath.row]]];
        [cell.contentView addSubview:bg1ImageView];
        
        [cell.contentView addSubview:[self set1Frame:CGRectMake(10+34+5, 10, screenMySize.size.width-100, payHight/2-10) setTitle:[tArray objectAtIndex:indexPath.row-1] setColor:YES setFont:YES]];
        
        if (indexPath.row == 1) {
            [cell.contentView addSubview:[self setFrame:CGRectMake(10+34+5, payHight/2, screenMySize.size.width-100, payHight/2-10) setTitle:[aArray objectAtIndex:indexPath.row-1] setColor:[UIColor redColor] setFont:YES]];
        }
        else
        [cell.contentView addSubview:[self setFrame:CGRectMake(10+34+5, payHight/2, screenMySize.size.width-100, payHight/2-10) setTitle:[aArray objectAtIndex:indexPath.row-1] setColor:[UIColor grayColor] setFont:YES]];
    }
    else
    {
        
        [cell.contentView addSubview:[self setFrame:CGRectMake(10, 0, screenMySize.size.width-100, payHight/2) setTitle:@"请选择支付方式" setColor:[UIColor blackColor] setFont:YES]];
        
        if (![[NSString stringWithFormat:@"%@",allString] isEqualToString:@"<null>"])
        {
            [cell.contentView addSubview:[self setFrame:CGRectMake(screenMySize.size.width-90, 0, 80, payHight/2) setTitle:[NSString stringWithFormat:@"%@元",allString] setColor:[UIColor redColor] setFont:NO]];
        }

    }
    
    return cell;
}

-(UILabel*)set1Frame:(CGRect)frame setTitle:(NSString*)title setColor:(BOOL)black setFont:(BOOL)font
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    if (font)
        titleLabel.textAlignment = 0;
    else
        titleLabel.textAlignment = 2;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+3];
    if (black)
        titleLabel.textColor = [UIColor blackColor];
    else
        titleLabel.textColor = [UIColor redColor];
    
    return titleLabel;
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setColor:(UIColor*)black setFont:(BOOL)font
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    if (font)
         titleLabel.textAlignment = 0;
    else
        titleLabel.textAlignment = 2;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize];
    titleLabel.textColor = black;
    
    return titleLabel;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    if (indexPath.row == 2)
    {
        
        if (urlStr != nil) {
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isPayView"];
            PayWebViewController *details = [[PayWebViewController alloc] init];
            details.UrlString = [NSString stringWithFormat:@"%@",urlStr];
            details.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:details animated:YES];
        }
        NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];

        [postDic setValue:orderId forKey:@"orderId"];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
            NSDictionary *ret = [SendService GetOrderPayingUrl:postDic];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSLog(@"订单支付接口 %@",ret);
                if ([[NSString stringWithFormat:@"%@",[ret objectForKey:@"Result"]] isEqualToString:@"success"])
                {
                    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isPayView"];
                    PayWebViewController *details = [[PayWebViewController alloc] init];
                    details.UrlString = [NSString stringWithFormat:@"%@",[ret objectForKey:@"Entity"]];
                    details.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:details animated:YES];
                }
                
            });
        });
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
